package com.unhz.taigu.service.impl;

import com.unhz.generator.dao.LoginInfoMapper;
import com.unhz.generator.pojo.LoginInfo;
import com.unhz.generator.utils.IsNumeric;
import com.unhz.generator.utils.Page;
import com.unhz.generator.vo.LoginInfoVo;
import com.unhz.taigu.service.LoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 曹显贵
 * @Description:
 * @Date: Created in 20:07 2018/1/25 0025
 * @Modified By:
 */
@Service
public class LoginInfoServiceImpl implements LoginInfoService {
    static final int PAGENUM = 1;
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Override
    public Page<LoginInfo> findPageAllLoginInfo(String pageNo, Integer pageSize, String keywords) {
        //分页查询
        int pageNum = PAGENUM;
        boolean numok = IsNumeric.isNumeric(pageNo);
        if(numok){
            if(pageNo !=null && !"".equals(pageNo)){
                pageNum = Integer.parseInt((pageNo));
            }
        }
        //查询记录条数(模糊查询用户得到ids)
        int totalRecords = loginInfoMapper.getTotalRecordsByKeywords("%"+keywords+"%");        //获取记录总条数
        Page page = new Page(pageNum,totalRecords);
        page.setPageSize(pageSize);
        Map<String ,Object> map = new HashMap<String,Object>();
        map.put("startIndex",page.getStartIndex());
        map.put("pageSize",page.getPageSize());
        map.put("keywords","%"+keywords+"%");
        //获取总记录数
        List<LoginInfoVo> records = loginInfoMapper.findLoginInfoMap(map);
        page.setRecords(records);
        String url = "/loginInfoController/findPageAllLoginInfo?keywords="+keywords;
        page.setUri(url);
        return page;
    }

    @Override
    public int deleteById(Integer loginInfoId) {
        return loginInfoMapper.deleteByPrimaryKey(Long.valueOf(loginInfoId));
    }

    @Override
    public int insertLoginInfo(LoginInfo loginInfo) {
        return loginInfoMapper.insertSelective(loginInfo);
    }
}
